<!-- 首页 -->
<template>
	<view class="page">
		<talk @menu="menu" :item="info"></talk>
		<speak ref="speak" @commentRemove="commentRemove" @thumb="thumb" @chat="chat" @reply="reply" :total="info.commentNum" :dataList="dataList"></speak>
		<view class="loadmore-box" v-if="dataList.length > 0">
			<u-loadmore font-size="24" :status="status" @loadmore="load"></u-loadmore>
		</view>
		<send-box :total="info.commentNum" :giveNum="info.giveNum" ref="send" @thumb="thumbSpeak" @send="send"></send-box>
		<u-back-top :scrollTop="scrollTop"></u-back-top>
		<coin-popup></coin-popup>
		<user-auth></user-auth>
		<user-phone></user-phone>
		<user-pay></user-pay>
		<cash-popup></cash-popup> 
		<u-action-sheet :list="sheetList" @click="clickSheet" v-model="sheetShow"></u-action-sheet>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			id: 0,
			userId: 0,
			noticeCommentId: '',
			noticeGiveId: '',
			info: {},
			background: {
				backgroundColor: '#fff',
			},
			form: {
				lon: '',
				lat: '',
			},
			status: 'loadmore',
			scrollTop: 0,
			isEnd: false,
			pageNum: 1,
			pageSize: 10,
			dataList: [],
			sheetList: [],
			sheetShow: false,
		};
	},
	onReachBottom() {
		console.log('到底');
		this.load();
	},
	onPageScroll(e) {
		this.onScrollTop(e);
	},
	onPullDownRefresh() {
		this.reload();
	},
	onLoad(option) {
		this.init(option);
	},
	// 分享小程序
	onShareAppMessage(res) {
		return {
			title: '送你一个对象，快来「处CP」吧！',
			path: '/pages/index?shareUserId='+this.userId,
			imageUrl: 'http://rbtnet.oss-cn-hangzhou.aliyuncs.com/test/20220914/58242695c2a8421b892eced5f1bd3b15.jpeg',
		};
	},
	onShareTimeline() {
		return {
			title: '送你一个对象，快来「处CP」吧！',
			path: '/pages/index?shareUserId='+this.userId,
			imageUrl: 'http://rbtnet.oss-cn-hangzhou.aliyuncs.com/test/20220914/58242695c2a8421b892eced5f1bd3b15.jpeg',
		}
	},
	methods: {
		init(option) {
			var userId = uni.getStorageSync("userId");
			this.userId = userId;
			this.id = option.dynamicId;
			if(option.noticeCommentId){
				this.noticeCommentId = option.noticeCommentId;
			}
			if(option.noticeGiveId){
				this.noticeGiveId = option.noticeGiveId;
			}
			this.getDetail();
			this.load();
		},
		load() {
			this.loadmore();
		},
		reload() {
			this.query();
		},
		query() {
			this.pageNum = 1;
			this.dataList = [];
			this.isEnd = false;
			this.loadmore();
		},
		async loadmore() {
			this.status = 'loading';
			if(this.isEnd){
				this.status = 'nomore';
				return;
			}
			var res = await this.getDataList();
			this.dataList = this.dataList.concat(res.rows);
			uni.stopPullDownRefresh();
		},
		getDetail() {
			this.$u.get('/wq/dynamic/getDetail',{
				id: this.id
			}).then(res => {
				this.info = res.data;
			})
		},
		commentRemove(commentId) {
			this.$u.get('/wq/comment/delete',{
				id: commentId
			}).then(res => {
				this.reload();
			})
		},
		async getDataList() {
			var res = await this.$u.get('/wq/comment/getList', {
				dynamicId: this.id,
				noticeCommentId: this.noticeCommentId,
				noticeGiveId: this.noticeGiveId,
				pageNum: this.pageNum,
				pageSize: this.pageSize,
			});
			var totalPage = Math.ceil(res.total/this.pageSize);
			this.pages = totalPage;
			this.pageNum ++;
			this.status = 'loadmore';
			if(this.pageNum > this.pages){
				this.status = 'nomore';
				this.isEnd = true;
			}
			return res;
		},
		onScrollTop(e) {
			this.scrollTop = e.scrollTop;
		},
		async loadquery() {
			
		},
		menu(e) {
			this.sheetList = e;
			this.sheetShow = true;
		},
		clickSheet(index) {
			var sheet = this.sheetList[index];
			if('remove' == sheet.tag){
				this.deleteTopic(sheet.id);
			}
		},
		deleteTopic(id) {
			this.$u.get('/wq/dynamic/delete',{
				id: id,
			}).then(res => {
				this.$refs.uToast.show({
					title: '删除成功',
					type: 'success',
					duration: 1000,
					url: 'pages/index/tabbar?PageCur=friend'
				});
			})
		},
		chat(e,i) {
			this.$refs.send.chat(e,i);
		},
		reply(e) {
			this.$refs.send.reply(e);
		},
		thumb(e,i) {
			this.$u.post('/wq/commentGive/add',{
				commentId: e.id,
			}).then(res => {
				if(res.data){
					this.dataList[i].giveNum = this.dataList[i].giveNum+1;
				}else{
					this.dataList[i].giveNum = this.dataList[i].giveNum-1;
				}
			});
		},
		thumbSpeak() {
			this.$u.post('/wq/dynamicGive/add',{
				dynamicId: this.id,
			}).then(res => {
				if(res.data){
					this.info.giveNum = this.info.giveNum + 1;
				}else{
					this.info.giveNum = this.info.giveNum - 1;
				}
			});
		},
		send(e,i) {
			this.judgeLogin((res) => {
				if(res.user.phonenumber == null || res.user.phonenumber == ''){
					this.$u.vuex('vuex_phone', true);
					return;
				}
				
				if(e.type == 'comment'){
					this.$u.post('/wq/comment/add',{
						dynamicId: this.id,
						content: e.text
					}).then(res => {
						this.query();
						this.$refs.send.close();
					});
				}else if(e.type == 'chat'){
					this.$u.post('/wq/reply/add',{
						commentId: e.data.id,
						content: e.text
					}).then(res => {
						this.$refs.speak.addUnshift(i, res.data);
						this.$refs.send.close();
					});
				}else if(e.type == 'reply'){
					this.$u.post('/wq/reply/add',{
						commentId: e.data.commentId,
						replyUserId: e.data.userId,
						content: e.text
					}).then(res => {
						this.$refs.speak.addUnshift(i, res.data);
						this.$refs.send.close();
					});
				}
			});
		},
	}
};
</script>
<style lang="scss" scoped>
	.page {
		padding-bottom: 80px;
	}
	.loadmore-box {
		padding: 10px 0;
	}
</style>
